//
//  SAShoppingCartHeaderView.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/5/20.
//

#import "SAShoppingCartHeaderView.h"
#import "SASalesOrderViewListModel.h"

@interface SAShoppingCartHeaderView ()

@property (nonatomic, strong) UIImageView *iconImageV;
@property (nonatomic, strong) UILabel *shopLab;
@property (nonatomic, strong) UILabel *priceLab;
@property (nonatomic, strong) UIButton *deleteBtn;

@end

@implementation SAShoppingCartHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        [self initUI];
    }
    return self;
}

-(void)setModel:(SASalesOrderViewListModel *)model {
    _model = model;
    NSString *newUrl = [[model.img componentsSeparatedByString:@","][0] stringByReplacingOccurrencesOfString:@"?x-oss-process=style/thumbnail" withString:@""];
    NSString *imageUrl = [NSString stringWithFormat:@"%@?x-oss-process=image/resize,l_100",newUrl];
    [self.iconImageV sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"shop_placeholderImg"]];
    self.shopLab.text = [NSString stringWithFormat:@"%@,%@",model.productCode,model.productName];
    if (model.isWarehousing == YES) {
        self.priceLab.text = [NSString stringWithFormat:@"入库价:%0.2lf",[model.entryPrice doubleValue]/100.f];
        NSString *productViewCostPrice = [NSString stringWithFormat:@"%@",[SAValueManger shareInStance].userInfo.menuList[1][@"perms"][@"productViewCostPrice"]];
        if ([productViewCostPrice isEqualToString:@"1"]) {
            self.priceLab.text = [NSString stringWithFormat:@"入库价:******"];
        }
    }else {
        self.priceLab.text = [NSString stringWithFormat:@"零售价:%0.2lf",[model.retailPrice doubleValue]/100.f];
    }
    
}

-(void)clickedDeleteBtn {
    !self.deleteBlock ?: self.deleteBlock();
}

-(void)clickedImageV {
    !self.showImageBlock ?: self.showImageBlock();
}

-(void)initUI {
    
    UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScaleNum(71))];
    whiteView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:whiteView];
    
    UIImageView *iconImageV = [[UIImageView alloc] initWithFrame:SARectMake(26, 6, 53, 59)];
    [self.contentView addSubview:iconImageV];
    iconImageV.contentMode = UIViewContentModeScaleToFill;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickedImageV)];
    iconImageV.userInteractionEnabled = YES;
    [iconImageV addGestureRecognizer:tap];
    self.iconImageV = iconImageV;
    
    [self.contentView addSubview:self.shopLab];
    [self.contentView addSubview:self.priceLab];
    [self.contentView addSubview:self.deleteBtn];

}

-(UILabel *)shopLab {
    if (!_shopLab) {
        _shopLab = [[UILabel alloc] initWithFrame:SARectMake(85, 19, 150, 15)];
        _shopLab.textColor = kBlackColor;
        _shopLab.font = kFont(13);
    }
    return _shopLab;
}

-(UILabel *)priceLab {
    if (!_priceLab) {
        _priceLab = [[UILabel alloc] initWithFrame:SARectMake(85, 37, 150, 15)];
        _priceLab.textColor = kLightgGrayColor;
        _priceLab.font = kFont(13);
    }
    return _priceLab;
}

-(UIButton *)deleteBtn {
    if (!_deleteBtn) {
        _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_deleteBtn setImage:[UIImage imageNamed:@"btn_delete"] forState:UIControlStateNormal];
        _deleteBtn.frame = SARectMake(270, 0, 58, 71);
        [_deleteBtn addTarget:self action:@selector(clickedDeleteBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteBtn;
}

@end
